const mongoose = require('mongoose');
const dbConfig = require('../config/db');
// 连接数据库 如果不自己创建 默认test数据库会自动生成
// mongoose.connect(`mongodb://${dbConfig.username}:${dbConfig.password}@${dbConfig.host}/${dbConfig.db}`); // 地址跟第一步的地址对应。
mongoose.connect(`mongodb://${dbConfig.host}:${dbConfig.port}/${dbConfig.db}`);
// 为这次连接绑定事件
const dbCont = mongoose.connection;
dbCont.once('error',(e) => console.log('Mongo connection error', e));
dbCont.once('open',() => console.log('Mongo connection successed'));
/************** 新增模式方法 **************/
function createSchema (name, schema) {
  return mongoose.model(name, mongoose.Schema(schema))
};
/************** 定义模型Model **************/
const ModelsConfig = {
  Login: {
    account : String,
    password : String
  },
  PlayList: {
    imgUrl: String,
    title: String,
    id: String,
    type: String,
    author: String,
    authorId: String,
    desc: String,
    collectNum: String,
    shareNum: String,
    commentNum: String,
    playNum: String,
    tags: [],
    songs: [],
    songIds: [],
  },
  Song: {
    id: String,
    name: String,
    commentThreadId: String,
    mvid: String,
    album: {},
    privilege: {},
    alias: [{}],
    score: Number,
    artists: [{}],
  },
};
let Models = {};
Object.keys(ModelsConfig).map(key => {
  Models[key] = createSchema(key, ModelsConfig[key]);
});
module.exports = Models;
